package com.example.urmsadmin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.urmsadmin.entity.SysOrder;
import com.example.urmsadmin.entity.SysUserWallet;
import com.example.urmsadmin.service.SysOrderService;
import com.example.urmsadmin.service.SysUserWalletService;
import com.example.urmsadmin.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@RequestMapping(value = "/sysDemo")
public class SysDemoController {

    @Autowired
    SysUserWalletService userWalletService;

    @Autowired
    SysOrderService orderService;

    //1.查询用户钱包余额
    @GetMapping("/queryBalanceByUserId/{userWalletId}")
    public CommonResult<Object> queryBalanceByUserId(@PathVariable Long userWalletId) {
        log.info("执行了===>SysDemoController中的queryBalanceByUserId方法");
        SysUserWallet userWallet = userWalletService.getOne(new QueryWrapper<SysUserWallet>().eq("user_wallet_id", userWalletId));
        if (userWallet != null) {
            return new CommonResult<>(200, "余额查询成功", userWallet);
        }
        return new CommonResult<>(233, "余额查询失败");
    }

    //2.用户消费100元的接口 3.用户退款20元接口
    @GetMapping("/walletOrder")
    public CommonResult<Object> walletOrder(@RequestParam(required = false) String transactionType,
                                            @RequestParam(required = false) BigDecimal transactionAmount,
                                            @RequestParam(required = false) Long userWalletId) {
        log.info("执行了===>SysDemoController中的walletOrder方法");
        SysUserWallet userWallet = userWalletService.getOne(new QueryWrapper<SysUserWallet>().eq("user_wallet_id", userWalletId));
        //0：充值 1：消费 2：退款 3：提现
        if ("0".equals(transactionType) || "2".equals(transactionType)) {
            userWallet.setBalance(userWallet.getBalance().add(transactionAmount));
        }else if("1".equals(transactionType) || "3".equals(transactionType)){
            BigDecimal balance = userWallet.getBalance();
            if(balance.compareTo(transactionAmount) == 1){
                userWallet.setBalance(balance.subtract(transactionAmount));
            }else {
                return new CommonResult<>(233, "余额不足！");
            }
        }else {
            return new CommonResult<>(233, "操作代码有误");
        }
        userWalletService.update(userWallet,new UpdateWrapper<SysUserWallet>().eq("user_wallet_id",userWalletId));
        SysOrder order = new SysOrder();
        order.setUserWalletId(userWalletId);
        order.setTransactionType(transactionType);
        order.setTransactionAmount(transactionAmount);
        order.setCreateTime(new Date());
        switch (transactionType){
            case "0":order.setRemark("充值");break;
            case "1":order.setRemark("消费");break;
            case "2":order.setRemark("退款");break;
            case "3":order.setRemark("提现");break;
        }
        orderService.save(order);
        return new CommonResult<>(200,"操作成功！");
    }

    //4.查询用户钱包金额变动明细的接口
    @GetMapping("/queryWalletDetail/{userWalletId}")
    public CommonResult<Object> queryWalletDetail(@PathVariable Long userWalletId) {
        log.info("执行了===>SysDemoController中的queryWalletDetail方法");
        List<SysOrder> order = orderService.list(new QueryWrapper<SysOrder>().eq("user_wallet_id", userWalletId).orderByAsc("create_time"));
        if (order != null) {
            return new CommonResult<>(200, "用户钱包金额变动明细查询成功", order);
        }
        return new CommonResult<>(233, "用户钱包金额变动明细查询失败");
    }

}
